package com.mxx.qh.utils;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;

import com.mxx.qh.interfaces.IException;
import com.mxx.qh.interfaces.ILoading;
import com.tencent.mm.opensdk.modelmsg.SendMessageToWX;
import com.tencent.mm.opensdk.modelmsg.WXMediaMessage;
import com.tencent.mm.opensdk.modelmsg.WXWebpageObject;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import java.io.ByteArrayOutputStream;
import java.util.List;

/**
 * @ClassName: WxShareUtils
 * @Description: java类作用描述
 * @Author: ljf89 (刘江飞)
 * @Date: 2021/1/11-- 15:59
 * @Email: 642371742@qq.com
 */
public class WxShareUtils {
    private static final int THUMB_SIZE = 150;
    private IWXAPI wxApi;

    /**
     * 分享网页类型至微信
     *
     * @param context 上下文
     * @param appId   微信的appId
     * @param webUrl  网页的url
     * @param title   网页标题
     * @param content 网页描述
     * @param bitmap  位图
     */
    public static void share(Context context, String appId
            , int type, String webUrl, String title, String content, Bitmap bitmap
            , ILoading iLoading, IException iException) {
        IWXAPI wxApi = WXAPIFactory.createWXAPI(context, appId);
        // 检查手机或者模拟器是否安装了微信
        boolean isInstallWx = isWeChatAppInstalled(context, wxApi);
        if (!isInstallWx) {
            iException.handlerException("您还没有安装微信");
            return;
        }
        WXWebpageObject webPage = new WXWebpageObject();
        webPage.webpageUrl = webUrl;
        WXMediaMessage msg = new WXMediaMessage(webPage);
        msg.title = title;
        msg.description = content;
        Bitmap thumbBmp = Bitmap.createScaledBitmap(bitmap, THUMB_SIZE, THUMB_SIZE, true);
        msg.thumbData = bmpToByteArray(thumbBmp, true);
        SendMessageToWX.Req req = new SendMessageToWX.Req();
        req.transaction = buildTransaction();
        req.message = msg;
        req.scene = getScene(type);
        iLoading.onFinish();
        wxApi.sendReq(req);
    }

    /**
     * 判断微信客户端是否存在
     *
     * @return true安装, false未安装
     */
    public static boolean isWeChatAppInstalled(Context context, IWXAPI api) {

        if (api.isWXAppInstalled()) {
            return true;
        } else {
            // 获取packageManager
            PackageManager packageManager = context.getPackageManager();
            if (packageManager == null) {
                return false;
            }
            // 获取所有已安装程序的包信息
            @SuppressLint("QueryPermissionsNeeded")
            List<PackageInfo> packageInfo = packageManager.getInstalledPackages(0);
            if (packageInfo != null) {
                for (int i = 0; i < packageInfo.size(); i++) {
                    String pn = packageInfo.get(i).packageName;
                    if (pn.equalsIgnoreCase("com.tencent.mm")) {
                        return true;
                    }
                }
            }
            return false;
        }
    }

    private static String buildTransaction() {
        return ("webpage" == null) ? String.valueOf(System.currentTimeMillis()) : "webpage" + System.currentTimeMillis();
    }

    private static int getScene(int type) {
        //SendMessageToWX.Req.WXSceneSession
        if (type == 1)//分享微信好友
        {
            return SendMessageToWX.Req.WXSceneSession;
        }
        if (type == 2)//分享微信朋友圈
        {
            return SendMessageToWX.Req.WXSceneTimeline;
        }
        return SendMessageToWX.Req.WXSceneSession;
    }

    public static byte[] bmpToByteArray(final Bitmap bmp, final boolean needRecycle) {
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        bmp.compress(Bitmap.CompressFormat.PNG, 100, output);
        if (needRecycle) {
            bmp.recycle();
        }

        byte[] result = output.toByteArray();
        try {
            output.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return result;
    }

}
